﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Capstone.Models;
using Capstone.Services;
using Capstone.ViewModels;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Capstone.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DayViewPage : ContentPage
    {
        DaysViewModel _viewModel;

        private string itemId;
        private string text;
        private string description;
        public string Id { get; set; }

        private int length;
        private string frequency;
        private string[] symptoms;
        private string[] othersymptoms;
        private string[] sleepsymptoms;
        public string CurrentId;
        public Item CurrentItem;
        public DayViewPage()
        {
            InitializeComponent();
            BindingContext = _viewModel = new DaysViewModel();
            CurrentId = ItemsViewModel.id;
            LoadItemId(CurrentId);
        }
        protected override void OnAppearing()
        {
            base.OnAppearing();
            _viewModel.OnAppearing();
        }

        protected bool SetProperty<T>(ref T backingStore, T value,
            [CallerMemberName] string propertyName = "",
            Action onChanged = null)
        {
            if (EqualityComparer<T>.Default.Equals(backingStore, value))
                return false;

            backingStore = value;
            onChanged?.Invoke();
            OnPropertyChanged(propertyName);
            return true;
        }

        public string Text
        {
            get
            {
                return text;
            }

            set
            {
                SetProperty(ref text, value);
            }
        }
        public int Length
        {
            get => length;
            set => SetProperty(ref length, value);
        }

        public string Frequency
        {
            get => frequency;
            set => SetProperty(ref frequency, value);
        }

        public string[] Symptoms
        {
            get => symptoms;
            set => SetProperty(ref symptoms, value);
        }

        public string[] OtherSymptoms
        {
            get => othersymptoms;
            set => SetProperty(ref othersymptoms, value);
        }

        public string[] SleepSymptoms
        {
            get => sleepsymptoms;
            set => SetProperty(ref sleepsymptoms, value);
        }
        public string Description
        {
            get => description;
            set => SetProperty(ref description, value);
        }

        public string ItemId
        {
            get
            {
                return itemId;
            }
            set
            {
                itemId = value;
                LoadItemId(value);
            }
        }

        public async void LoadItemId(string itemId)
        {
            try
            {
                var item = await DependencyService.Get<IDataStore<Item>>().GetItemAsync(itemId);
                Id = item.Id;
                Text = item.Text;
                Description = item.Description;
                Frequency = item.Frequency;
                Length = item.Length;
                Symptoms = item.Symptoms;
                OtherSymptoms = item.OtherSymptoms;
                SleepSymptoms = item.SleepSymptoms;
                CurrentItem = item;
            }
            catch (Exception)
            {
                Debug.WriteLine("Failed to Load Item");
            }
        }
    }
}